package com.yaoayao.mall.all.controller;

import com.yaoayao.mall.cart.client.CartFeignClient;
import com.yaoayao.mall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author: peiyao
 * @date: 2024/4/16 3:27
 * @description:
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes) {
        //返回值 库存对象
        CartInfo cartInfo = cartFeignClient.addCart(skuId,skuNum);
        //cartInfo.setSkuNum(skuNum);
        //model.addAttribute("cartInfo",cartInfo);
        redirectAttributes.addAttribute("pid",skuId);
        redirectAttributes.addAttribute("sn",skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";
        //http://cart.gmall.com/toCart.html

    }

    //重定向到 购物车添加成功页面
    @GetMapping("/toCart.html")
    public String toCart(Long pid, Integer sn, Model model){
        //返回值 库存对象
        CartInfo cartInfo = cartFeignClient.toCart(pid);
        if(null != cartInfo){
            cartInfo.setSkuNum(sn);
            model.addAttribute("cartInfo",cartInfo);
            return "cart/addCart";
        }else{
            //之前未登录时有购物车 用户突然登录了 没有购物车 导致空指针异常  走首页
            return "redirect:http://www.gmall.com";
        }
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){
        return "cart/index";
    }
}
